<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 专辑信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-delete"
            class="handle-del mr10"
            @click="delAllSelection"
        >批量删除</el-button>
        <el-input v-model='query.aname'  placeholder="专辑名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addAlbum">添加专辑</el-button>

      </div>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="aid" label="专辑编号" width="55" align="center"></el-table-column>
        <el-table-column prop="aname" label="专辑名"></el-table-column>
          <el-table-column prop="author" label="作者"></el-table-column>
          <el-table-column prop="tname" label="类型"></el-table-column>
          <el-table-column prop="cover" label="封面图">
              　<template slot-scope="scope">
              　　　　<img :src="require('../../assets/img/'+scope.row.cover)"  width="40" height="40" class="head_pic"/>
              　　</template>
          </el-table-column>
          <el-table-column   prop="updstate" label="更新状态" :formatter='formupdstate'></el-table-column>
          <el-table-column   prop="state" label="审批状态" :formatter='formstate'></el-table-column>
          <el-table-column   prop="uploadname" label="上传用户" ></el-table-column>
          <el-table-column   prop="uploadid" label="上传身份"  :formatter='formid' ></el-table-column>
          <el-table-column   prop="remark" label="备注"  width='300'></el-table-column>
          <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, JSON.parse(JSON.stringify(scope.row)))"
            >编辑
            </el-button>
            <el-button
                type="text"
                icon="el-icon-delete"
                class="red"
                @click="handleDelete(scope.$index, scope.row)"
            >占定
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
<!--        // layout prev 左箭头 pager 页数 next 右箭头
        //@current-change 页数改变时间 有一个参数 就是当前页数
        //total 条数 会根据总条数和每页显示的条数进行 分页-->
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="preNext"
            :page-size='pagesize'
            :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog :title='title' :visible.sync='editVisible' width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="专辑名">
          <el-input v-model="form.aname"></el-input>
        </el-form-item>
          <el-form-item label="作者">
              <el-input v-model="form.author"></el-input>
          </el-form-item>
          <el-form-item label='类型'>
              <template>
                  <el-select  v-model="form.typeid" placeholder="请选择">
                      <el-option
                          v-for="item in type"
                          :label="item.tname"
                          :value="item.tid">
                      </el-option>
                  </el-select>
              </template>
          </el-form-item>
          <el-form-item v-show='uploadShow'>
              <template>
                  <div>
              <el-upload
                  class="upload-demo"
                  action="http://localhost:8090/album/upload"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :before-remove="beforeRemove"
                  :data='uploaddata'
                  :on-success='success'
                  list-type="picture"
                  :headers='myHeaders'
                  multiple
                  :limit="3"
                  :on-exceed="handleExceed"
                  :file-list="fileList">
                  <el-button size="small" type="primary">点击上传图片</el-button>
                  <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
                  </div>
              </template>
          </el-form-item>
          <el-form-item label="连载状态">
              <el-select v-model='form.updstate'>
                  <el-option label='连载' value='1'>
                  </el-option>
                  <el-option label='完结' value='2'>
                  </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="备注">
              <el-input v-model="form.remark"></el-input>
          </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "albumtable",
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
      total: 6, pagesize:5,
      form: {},
      idx: -1,
      id: -1,
        title:[],
        type:[],
        imgsrc:[],
        fileList: [],
        uploaddata:{},
        uploadShow:true,
        myHeaders: {token:sessionStorage.getItem('token')}
    };
  },
  created() {
    this.getData();
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
        //员工列表
     this.$http({
       url:'http://localhost:8090/album/selectAlbum',
       method:'post',
       params:{pageNum:1,pageSize:5,aname:this.query.aname}
     }).then(response=>{
         //console.log(response)
       //console.log(response)
       this.tableData=response.list
         this.total=response.total
         this.pagesize=response.pageSize
     })
        //获取类型列表
        this.$http({
            url:'http://localhost:8090/album/selectType',
            method:'post',
        }).then(res=>{
            this.type=res
        })
    },formupdstate(row){return row.updstate===1?'连载中':'已完结'},formstate(row){
        if(row.state==1){
            return '成功';
        }else if(row.state==2){
            return '失败'
        }else {
            return '待审批'
        }
      },formid(row){
        return row.uploadid==1?'用户':'员工';
      },
      //数据转换
    // 触发搜索按钮
    handleSearch() {
      this.$http({
        url:'http://localhost:8090/album/selectAlbum',
        method:'post',
        params:{pageNum:1,pageSize:5,aname:this.query.aname}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })
        //添加
    },addAlbum(){
          this.title='添加专辑'
          this.uploadShow=false
          this.form=[];
          this.editVisible=true;
      }
    ,
    // 删除操作
    handleDelete(index) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
          .then(() => {
            this.$message.success("删除成功");
            this.tableData.splice(index, 1);
          })
          .catch(() => {});
    },
      // 多选操作
      handleSelectionChange(val) {
          this.multipleSelection = val;
      },
    delAllSelection() {
        let str="";
        for (let i=0;i<this.multipleSelection.length;i++){
            str+=this.multipleSelection[i].aid+","
        }
        console.log(str)
        this.$http({
            url:'/album/delAlbum',
            method:'post',
            params:{str:str}
        }).then(res=>{
            if(res==true){
                this.$message.success('删除成功')
                this.getData()
            }else {
                this.$message.error('删除失败该专辑下还有数据')
            }

        })
    },
    // 编辑操作
    handleEdit(index, row) {
        //文件上传传递的参数
        this.uploaddata={aid:row.aid}
        this.uploadShow=true
        this.title='编辑';
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {


    },
      submit(){//动态提交
        if(this.form.aid==undefined){
            this.$http({
                url:'http://localhost:8090/album/albumAdd',
                method:'post',
                params:{aname:this.form.aname,author:this.form.author,typeid:this.form.typeid,
                    updstate:this.form.updstate,remark:this.form.remark,uploadname:sessionStorage.getItem('eusername')
                }
            }).then(response=>{
                if(response==true){
                    this.$message.success('添加成功')
                    this.editVisible = false;
                    this.getData()
                }else {
                    this.$message.error('添加失败')
                }
            })
        }else {
            this.$http({
                url:'http://localhost:8090/album/updAlbum',
                method:'post',
                params:{aid:this.form.aid,aname:this.form.aname,author:this.form.author,typeid:this.form.typeid,
                    updstate:this.form.updstate,remark:this.form.remark
                }
            }).then(res=>{
                if(res===true){
                    this.$message.success("修改成功")
                    this.getData()
                }else {
                    this.$message.warning("修改失败")
                }
            })
            this.editVisible = false;
        }
      }
      ,
    // 分页导航
    handlePageChange() {
      this.getData();
    },preNext(val){
      this.$http({
        url:'http://localhost:8090/album/selectAlbum',
        method:'post',
        params:{pageNum:val,pageSize:5,aname:this.query.aname}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })

    }, handleRemove(file, fileList) {
          console.log(file, fileList);
      },
      handlePreview(file) {
          console.log(file);
      },
      handleExceed(files, fileList) {
          this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
      },
      beforeRemove(file, fileList) {
          return this.$confirm(`确定移除 ${ file.name }？`);
      },success(res,file,fileList){
          if(res!=false){
              this.$message.success('封面上传成功')
          }else {
              this.$message.success('上传失败zzz')
          }

      }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
